$def with (authors, name_path, dict_path)
$# :param list[dict] authors: e.g. {name: str, key: str}
$# :param str name_path: form path for the user-visible author name
$# :param str dict_path: form path for the author dict

$jsdef render_author_autocomplete_item(item):
    $if item.key == "__new__":
        <div class="ac_author ac_addnew" title="Add a new author">
            <span class="action">$_('Create a new record for') </span>
            <span class="name">$item.name</span>
        </div>
    $else:
        <div class="ac_author" title="Select this author">
            <span class="name">
                $item.name
                $if item.birth_date or item.death_date:
                    (${item.birth_date or ' '}&ndash;${item.death_date or ' '})
            </span>
            <span class="olid">$item.key.split('/')[2]</span>
            &bull;
            $if item.work_count == 0:
                <span class="work">No books associated with $item.name</span>
            $elif item.work_count == 1:
                <span class="books">1 book</span>
                <span class="work">titled <i>$item.works[0]</i></span>
            $else:
                <span class="books">$item.work_count books</span>
                <span class="work">including <i>$item.works[0]</i></span>

            $if item.subjects:
                <span class="subject">Subjects: ${', '.join(item.subjects)}</span>
        </div>

$jsdef render_author(name_path, dict_path, disabled, i, author):
    <div class="input">
        <input name="$name_path--$i" class="author author-autocomplete" type="text" id="author-$i" value="$author.name" $cond(disabled, 'disabled', '')>
        <input name="$dict_path--$i--author--key" type="hidden" id="author-$i-key" value="$author.key" />
        <a href="javascript:;" class="remove red plain" title="$_('Remove this author')">[&times;]</a>&nbsp;
        <a href="javascript:;" class="add">$_("Add another author?")</a>
    </div>

<div id="authors">
    $for author in (authors or [storage(name="", key="")]):
        $:render_author(name_path, dict_path, True, loop.index0, author)
</div>
<script type="text/javascript">
window.q.push( function () {
    \$('.author-autocomplete').prop('disabled', false );
    \$("#authors").setup_multi_input_autocomplete(
        "input.author-autocomplete",
        render_author.bind(null, "$name_path", "$dict_path", false),
        {
            endpoint: "/authors/_autocomplete",
            // Don't render "Create new author" if searching by key
            addnew: function(query) { return !/^OL\d+A/i.test(query); },
        },
        {
            minChars: 2,
            max: 11,
            matchSubset: false,
            autoFill: false,
            formatItem: render_author_autocomplete_item
        });
} );
</script>
